<?
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('helper.inc');
require_once('crypt.inc');
require_once('settings.inc');
require_once('menu.inc');
require_once('knumber.inc');

$year = $_REQUEST["year"];
$month = $_REQUEST["month"];
$today = $_REQUEST["today"];
$func = $_REQUEST["func"];

$db = OpenDB();

if (isset($func) && ($func == "save" || $func == "update")) {
   if ($func == "update")
      $ca_num = $_REQUEST["ca_num"];

   $ca_prnum = $_REQUEST["ca_prnum"];
   $ca_task = $_REQUEST["ca_task"];
   $ca_minum = $_REQUEST["ca_minum"];
   $ca_date = $_REQUEST["ca_date"];
   $ca_end = $_REQUEST["ca_end"];
   $ca_title = $_REQUEST["ca_title"];
   $ca_text = $_REQUEST["ca_text"];
   $ca_priority = $_REQUEST["ca_priority"];
   $err = "";

   if (!isset($ca_prnum))
      $ca_prnum = 0;

   if (!isset($ca_task))
      $ca_task = 0;

   if (!isset($ca_minum))
      $ca_minum = 0;

   if (!isset($ca_date))
      $err .= "Sie haben kein Startdatum angegeben!<br>\n";

   if (!isset($ca_title))
      $err .= "Sie m&uuml;ssen unbedingt einen Titel angeben!<br>\n";

   $indate = $ca_date;
   $darr = explode(".", $ca_date);

   if (!checkdate($darr[1], $darr[0], $darr[2]))
      $err .= "Sie haben ein ung&uuml;ltiges Startdatum angegeben!<br>\n";
   else
      $ca_date = gmmktime(0, 0, 0, $darr[1], $darr[0], $darr[2]);

   $enddate= $ca_end;

   if (isset($ca_end)) {
      $darr = explode(".", $ca_end);

      if (!checkdate($darr[1], $darr[0], $darr[2]))
         $err .= "Sie haben ein ung&uuml;ltiges Endedatum angegeben!<br>\n";
      else
         $ca_end = gmmktime(0, 0, 0, $darr[1], $darr[0], $darr[2]);
   }
   else
      $ca_end = 0;

   if (!isset($ca_text))
      $ca_text = "";

   if (strlen($err) > 0) {
      closeDB($db);
      Error($err);
      echo "<form name=\"datum\" action=\"calentry.php\" method=\"post\">\n";
      $darr = explode(".", $indate);
      echo "<input type=\"hidden\" name=\"today\" value=\"" . $darr[0] . "\">\n";
      echo "<input type=\"hidden\" name=\"month\" value=\"" . $darr[1] . "\">\n";
      echo "<input type=\"hidden\" name=\"year\" value=\"" . $darr[2] . "\">\n";

      if ($func == "update")
         echo "<input type=\"hidden\" name=\"ca_num\" value=\"$ca_num\">\n";

      echo "<input type=\"hidden\" name=\"ca_prnum\" value=\"$ca_prnum\">\n";
      echo "<input type=\"hidden\" name=\"ca_task\" value=\"$ca_task\">\n";
      echo "<input type=\"hidden\" name=\"ca_minum\" value=\"$ca_minum\">\n";
      echo "<input type=\"hidden\" name=\"ca_title\" value=\"$ca_title\">\n";
      echo "<input type=\"hidden\" name=\"ca_text\" value=\"$ca_text\">\n";
      echo "<input type=\"hidden\" name=\"ca_date\" value=\"$indate\">\n";
      echo "<input type=\"hidden\" name=\"ca_end\" value=\"$enddate\">\n";
      echo "<input type=\"hidden\" name=\"ca_priority\" value=\"$ca_priority\">\n";

      if (isset($func) && $func == "plan")
         echo "<input type=\"hidden\" name=\"func\" value=\"plan\">\n";

      echo "<input type=\"hidden\" name=\"header\" value=\"1\">\n";
      echo "<input type=\"hidden\" name=\"menu\" value=\"0\">\n";
      echo "<table class=\"indent\"><tr><td>\n";
      Button("<-- Zur&uuml;ck", "datum");
      echo "</td></tr></table>\n";
      echo "</form>\n";
      include('footer.inc');
      return;
   }

   if (isset($ca_prnum) && $ca_prnum > 0) {
      $query = "select pr_pl from project where pr_num = $ca_prnum";
      $result = QueryDB($db, $query);

      if (!$result) {
         closeDB($db);
	 return;
      }

      $data = fetchDB($result, 0);
      $ca_writer = $data[0];
   } else
      $ca_writer = $unum;

   if ($func != "update") {
      $query = "select co_calendar from counter";
      $result = QueryDB($db, $query);

      if (!$result) {
         closeDB($db);
         return;
      }

      $data = fetchDB($result, 0);
      $co_calendar = $data[0];
      $co_calendar++;

      $query = "insert into calendar (ca_num,";

      if ($ca_prnum > 0)
         $query .= "ca_prnum,";

      if ($ca_minum > 0)
         $query .= "ca_minum,";

      if ($ca_task > 0)
         $query .= "ca_task,";

      $query .= "ca_date, ca_title, ca_text, ca_status, ca_end, ca_writer, ";
      $query .= "ca_repeat, ca_repanz, ca_priority) values ($co_calendar,";

      if ($ca_prnum > 0)
         $query .= "$ca_prnum,";

      if ($ca_minum > 0)
         $query .= "$ca_minum,";

      if ($ca_task > 0)
         $query .= "$ca_task,";

      $query .= "$ca_date, '$ca_title', '$ca_text',";
      $query .= "1, $ca_end, $ca_writer, 0, 0, $ca_priority)";
   } else {
      $query = "update calendar set ";
      $beistr = "";

      if ($ca_prnum > 0) {
         $query .= "ca_prnum = $ca_prnum";
	 $beistr = ",";
      }

      if ($ca_minum > 0) {
         $query .= "$beistr ca_minum = $ca_minum";
	 $beistr = ",";
      }

      if ($ca_task > 0) {
         $query .= "$beistr ca_task = $ca_task";
         $beistr = ",";
      }

      $query .= "$beistr ca_date = $ca_date, ca_title = '$ca_title',";
      $query .= "ca_text = '$ca_text', ca_end = $ca_end, ca_priority = $ca_priority ";
      $query .= "where ca_num = $ca_num";
   }

   QueryDB($db, "begin");

   if (!QueryDB($db, $query)) {
      QueryDB($db, "rollback");
      closeDB($db);
      return;
   }

   if ($func != "update") {
      $query = "update counter set co_calendar = $co_calendar";

      if (!QueryDB($db, $query)) {
         QueryDB($db, "rollback");
         closeDB($db);
         return;
      }
   }

   QueryDB($db, "commit");
   echo "<p class=\"cry\">Eintrag wurde erfolgreich gespeichert!</p>\n";
   echo "<form name=\"datum\" action=\"appt.php\" method=\"post\">\n";
   $darr = explode(".", $indate);
   echo "<input type=\"hidden\" name=\"today\" value=\"" . $darr[0] . "\">\n";
   echo "<input type=\"hidden\" name=\"month\" value=\"" . $darr[1] . "\">\n";
   echo "<input type=\"hidden\" name=\"year\" value=\"" . $darr[2] . "\">\n";
   echo "<input type=\"hidden\" name=\"header\" value=\"1\">\n";
   echo "<input type=\"hidden\" name=\"menu\" value=\"0\">\n";
   echo "<table class=\"indent\"><tr><td>\n";
   Button("Weiter -->", "datum");
   echo "</td></tr></table>\n";
   echo "</form>\n";
   include('footer.inc');
   return;
}

$ca_num = $_REQUEST['ca_num'];
$ca_prnum = $_REQUEST['ca_prnum'];
$ca_task = $_REQUEST['ca_task'];
$ca_minum = $_REQUEST['ca_minum'];
$ca_date = $_REQUEST['ca_date'];
$ca_end = $_REQUEST['ca_end'];
$ca_title = $_REQUEST['ca_title'];
$ca_text = $_REQUEST['ca_text'];
$ca_writer = $_REQUEST['ca_writer'];
$ca_priority = $_REQUEST['ca_priority'];

echo "<form name=\"Cal\" action=\"calentry.php\" method=\"post\">\n";

if (isset($func) && $func == "edit") {
   echo "<input type=\"hidden\" name=\"func\" value=\"update\">\n";
   echo "<input type=\"hidden\" name=\"ca_num\" value=\"$ca_num\">\n";
} else
   echo "<input type=\"hidden\" name=\"func\" value=\"save\">\n";

echo "<input type=\"hidden\" name=\"header\" value=\"1\">\n";
echo "<input type=\"hidden\" name=\"menu\" value=\"0\">\n";

if (isset($func) && $func == "plan") {
   echo "<input type=\"hidden\" name=\"ca_prnum\" value=\"$ca_prnum\">\n";
   echo "<input type=\"hidden\" name=\"ca_task\" value=\"$ca_task\">\n";
}

echo "<table class=\"input\">\n";
echo "<tr><th class=\"sel\" colspan=2>";

if (isset($ca_num) && $ca_num > 0)
   echo "Kalendereintrag editieren";
else
   echo "Neuer Kalendereintrag";

echo "</th></tr>\n";
// Anzeigen der Projekte fuer die ein Kalendertask erfasst werden kann.
// Die angezeigten Projekte haengen von der Berechtigungsstufe ab.
echo "<tr><td>Projekt:</td><td>\n";

if ($rstufe == 1 || $rstufe == 2) {
   $query = "select pr_num, pr_name from project where ";

   if ($rstufe == 1) {
      $query .= "pr_status = 0 ";
   } else if ($rstufe == 2) {
      $query .= "pr_status = 0 and pr_pl = $unum";
   }
} else if ($rstufe >= 3) {
   $query = "select distinct al_ressource, ta_plnum, pl_prnum, pr_name from ";
   $query .= "allocation, task, plan, project ";
   $query .= "where task.ta_num = allocation.al_task and ";
   $query .= "plan.pl_num = task.ta_plnum and project.pr_num = plan.pl_prnum";
}

if ($rstufe >= 3)
   $query .= " order by pl_prnum";
else
   $query .= " order by pr_num";

$result = QueryDB($db, $query);

if (!$result) {
   closeDB($db);
   include('footer.inc');
   return;
}

$anz = numrowsDB($result);

if (!$anz) {
   echo "--- Keine Projekte ---";
} else {
   echo "<select ";

   if (isset($func) && $func == "plan")
      echo "disabled ";

   echo "onChange=\"javascript:MoveTo('calentry.php','ca_prnum='+this.value+'&header=1&menu=0')\" name=\"ca_prnum\">\n<option value=0>-- kein Projekt --</option>\n";
   $i = 0;

   while ($i < $anz) {
      $data = fetchDB($result, $i);

      if ($rstufe == 3) {
         $pr_num = $data[2];
	 $pr_name = $data[3];
      } else {
         $pr_num = $data[0];
	 $pr_name = $data[1];
      }

      # Pruefen, ob es sich um ein laufendes Projekt handelt
      $query = "select count(*) from plan where pl_status = 6 and pl_prnum = $pr_num";

      if (!($resco = QueryDB($db, $query))) {
         closeDB($db);
         include('footer.inc');
         return;
      }

      $data = fetchDB($resco, 0);

      if ($data[0] > 0) {
         $i++;
         continue;
      }

      if (isset($ca_prnum) && $ca_prnum == $pr_num)
         echo "<option selected value=\"$pr_num\">$pr_num $pr_name</option>\n";
      else
         echo "<option value=\"$pr_num\">$pr_num $pr_name</option>\n";

      $i++;
   }

   echo "</select>\n";
}

echo "</td></tr>\n";
// Aussuchen eines Tasks. Es werden nur jene Tasks angezeigt, welche
// dem zuvor ausgewaehlten Projekt entsprechen. Wurde kein Projekt
// gewaehlt ist diese Auswahl deaktiviert!
echo "<tr><td>Task:</td><td>";

if (isset($ca_prnum) && $ca_prnum > 0) {
   // Ermitteln der Plannummer
   $query = "SELECT pl_num, pl_lfd FROM plan WHERE pl_prnum = $ca_prnum ";
   $query .= "ORDER BY pl_lfd desc";
   $result = QueryDB($db, $query);

   if (!$result) {
      closeDB($db);
      return;
   }

   if (numrowsDB($result) > 0) {
      $data = fetchDB($result, 0);
      $pl_num = $data[0];

      echo "<select ";

      if (isset($func) && $func == "plan")
         echo "disabled ";

      echo "onChange=\"javascript:MoveTo('calentry.php','ca_prnum=$ca_prnum&ca_task='+this.value+'&header=1&menu=0')\" name=\"ca_task\">\n";

      if (isset($ca_task) && $ca_task > 0) {
         $query = "select ta_start, ta_duration from task where ";
	 $query .= "ta_num = $ca_task";
	 $result = QueryDB($db, $query);

	 if (!$result) {
	    closeDB($db);
	    return;
	 }

	 $data = fetchDB($result, 0);
	 $ta_start = $data[0];
	 $ta_duration = $data[1];
         $sel = $ca_task;
      }
      else
         $sel = 0;

      echo "<option value=\"0\">-- kein Task --</option>\n";
      SelectTask($pl_num, 0, $sel);
      echo "</select>\n";
   } else
      echo "&nbsp;";
} else {
   echo "&nbsp;";
}

echo "</td></tr>\n";
// Aussuchen eines Mitarbeiters dem der Kalendereintrag zugeordnet werden
// soll. Wurde zuvor ein Task ausgewaehlt, werden hier nur die dem Task
// zugeordneten Mitarbeiter angezeigt.
echo "<tr><td>Mitarbeiter:</td><td>";
if (isset($ca_task) && $ca_task > 0) {
   $query = "select distinct al_ressource, mi_vname, mi_nname from allocation,";
   $query .= "mitarbeiter where al_task = $ca_task and mi_num = al_ressource ";
   $query .= "order by mi_nname, mi_vname";
} else if ($rstufe >= 3) {
   $query = "select mi_num, mi_vname, mi_nname from mitarbeiter ";
   $query = "where mi_num = $unum";
} else {
   $query = "select mi_num, mi_vname, mi_nname from mitarbeiter ";
   $query .= "order by mi_nname, mi_vname";
}

$result = QueryDB($db, $query);

if (!$result) {
   closeDB($db);
   include('footer.inc');
   return;
}

echo "<select name=\"ca_minum\">";

if ($rstufe < 2)
   echo "<option value=\"0\">-- kein Mitarbeiter --</option>\n";

$anz = numrowsDB($result);
$i = 0;

while ($i < $anz) {
   $data = fetchDB($result, $i);
   $mi_num = $data[0];
   $mi_vname = $data[1];
   $mi_nname = $data[2];
   echo "<option ";

   if (isset($ca_minum) && $ca_minum > 0 && $ca_minum == $mi_num)
      echo "selected ";

   echo "value=\"$mi_num\">$mi_nname $mi_vname</option>\n";
   $i++;
}

echo "</select></td></tr>\n";
echo "<tr><td>Priorit&auml;t:</td><td>";
echo "<select name=\"ca_priority\">\n";

for ($i = 0; $i < 3; $i++) {
   echo "<option ";

   if ($i == $ca_priority)
      echo "selected ";

   switch ($i) {
      case 0: echo "value=\"$i\">Niedrig</option>\n"; break;

      case 1: if (!isset($ca_priority))
		 echo "selected ";

	      echo "value=\"$i\">Normal</option>\n";
      break;

      case 2: echo "value=\"$i\">Hoch</option>\n"; break;
   }
}

echo "</select></td></tr>\n";
// Eingabe des Startdatums.
// Wurde ein Task ausgewaehlt, dann darf dieses Datum nicht kleiner dem
// Startdatum des Tasks sein und nicht groesser dem Endedatum des Tasks!
echo "<tr><td>Startdatum:</td>";

if (isset($ca_date))
   $datum = $ca_date;
else
   $datum = gmdate($dtformatshort, time());

$vd = GetVisualDate($dtformatshort);
echo "<td><input type=\"text\" name=\"ca_date\" value=\"$datum\" size=10 maxlength=10>
          <input type=\"button\" value=\"...\" onClick=\"javascript:open_window('calendar.php?header=1&target=window.opener.document.Cal.ca_date', 'Calendar', 200, 250)\">
      &nbsp;&nbsp;($vd)</td></tr>\n";
// Eingabe des Endedatums. Dieses kann auch leer bleiben.
// Wurde ein Task ausgewaehlt, dann darf dieses Datum nicht kleiner dem
// Startdatum des Tasks sein und nicht groesser dem Endedatum des Tasks!
echo "<tr><td>Endedatum:</td>";

echo "<td><input type=\"text\" name=\"ca_end\" value=\"$ca_end\" size=10 maxlength=10>
          <input type=\"button\" value=\"...\" onClick=\"javascript:open_window('calendar.php?header=1&target=window.opener.document.Cal.ca_end', 'Calendar', 200, 250)\">
      &nbsp;&nbsp;($vd)</td></tr>\n";
// Eingabe des Titels. Es muss ein Titel (Ueberschrift) angegeben werden!
echo "<tr><td>Titel:</td>";

if (!isset($ca_title) && isset($func) && $func == "plan") {
   $query = "select ta_name from task where ta_num = $ca_task";
   $result = QueryDB($db, $query);
   $data = fetchDB($result, 0);
   $ca_title = $data[0];
}

echo "<td><input type=\"text\" name=\"ca_title\" value=\"$ca_title\" size=60 maxlength=60></td></tr>\n";
// Eingabe eines freien Textes.
echo "<tr><td valign=\"top\">Text:</td>";
echo "<td><textarea name=\"ca_text\" rows=10 cols=60>$ca_text</textarea></td></tr>\n";
echo "<table class=\"indent\"><tr><td>\n";
ButtonSubmit("Speichern", "Cal");
echo "</td></tr></table>\n";	// class = indent!
echo "</td></tr></table>\n";
echo "</form>\n";

include('footer.inc');
